package com.cn.zpark.filter;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @PackageName: com.cn.zpark.filter
 * @ClassName: FilterDemo01
 * @Description:
 * @author: RZS
 * @date: 2022/4/14  15:35
 */
@WebFilter("*.do")
public class FilterDemo01 implements Filter {
    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        HttpServletRequest req = (HttpServletRequest) request;
        HttpServletResponse resp = (HttpServletResponse) response;

        // 设置请求缓冲区
        req.setCharacterEncoding("utf-8");
        // 设置响应内容及格式
        resp.setContentType("text/html;charset=utf-8");

        System.out.println("FilterDemo01过滤器");

        // 放行，访问后面的资源
        chain.doFilter(req, resp);
    }

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        System.out.println("Filter初始化成功");
    }

    @Override
    public void destroy() {
        System.out.println("Filter销毁");
    }
}
